import 'package:flutter/material.dart';
import 'package:flutter_sunmi_uvc_camera/flutter_sunmi_uvc_camera.dart';
import 'package:get/get.dart';

import 'index.dart';

class DemoPage extends StatefulWidget {
  const DemoPage({Key? key}) : super(key: key);

  @override
  State<DemoPage> createState() => _DemoPageState();
}

class _DemoPageState extends State<DemoPage>
    with AutomaticKeepAliveClientMixin {
  @override
  bool get wantKeepAlive => true;

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return const _DemoViewGetX();
  }
}

class _DemoViewGetX extends GetView<DemoController> {
  const _DemoViewGetX({Key? key}) : super(key: key);

  // 主视图
  Widget _buildView() {
    return Container(
      decoration: const BoxDecoration(
        color: Colors.grey,
      ),
      width: double.infinity,
      height: double.infinity,
      child: LayoutBuilder(
        builder: (context, constraints) {
          return Stack(
            fit: StackFit.expand,
            children: [
              UVCCameraView(
                cameraController: controller.cameraController,
                params:
                    const UVCCameraViewParamsEntity(frameFormat: 0, maxFps: 30),
                width: constraints.biggest.width,
                height: constraints.biggest.height,
              ),
              SizedBox(
                width: constraints.biggest.width,
                height: constraints.biggest.height,
                child: Obx(
                  () {
                    if (controller.detectedFace == null ||
                        controller.detectedFace?.size == null) {
                      return const SizedBox.shrink();
                    }
                    return CustomPaint(
                      painter: FacePainter(
                        wellPositioned:
                            controller.detectedFace?.wellPositioned ?? false,
                        isRealPerson:
                            controller.detectedFace?.isRealPerson ?? false,
                        face: controller.detectedFace!.face,
                        indicatorShape: IndicatorShape.defaultShape,
                        imageSize: controller.detectedFace!.size!,
                      ),
                    );
                  },
                ),
              ),
              Obx(() {
                if (controller.imageBytes == null) {
                  return const SizedBox.shrink();
                } else {
                  return Positioned(
                    right: 0,
                    child: Column(
                      children: [
                        SizedBox(
                          width: 80,
                          height: 120,
                          child: Image.memory(
                            controller.imageBytes!,
                            fit: BoxFit.fill,
                            width: double.infinity,
                            height: double.infinity,
                          ),
                        ),
                        Text(
                          'length:${controller.imageBytes!.length}',
                          style: const TextStyle(
                            color: Colors.white,
                            fontSize: 16,
                          ),
                        ),
                      ],
                    ),
                  );
                }
              }),
            ],
          );
        },
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<DemoController>(
      init: DemoController(),
      id: "demo",
      builder: (_) {
        return Scaffold(
          body: SafeArea(
            child: _buildView(),
          ),
        );
      },
    );
  }
}
